package com.ipharma.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.config.Config;
import com.ipharma.dto.Product;

/**
 * A Swing-based top level window class.
 * <P>
 * @author JorgE.H
 */
public class GUIReturn extends GUIOperation
{
  /**
   * The Layouts.
   */
  GridBagLayout layoutBase = new GridBagLayout();

  /**
   * The main panel.
   */
  GUIButtonPanel jPanelButtons;
  GUITransactionReturn jPanelTransactions;
  GUIBarCode jPanelBarCode;
  GUIButtonImage jButtBlank;
  
  /**
   * Attributes
   */

  /**
   * Constructs a new instance.
   */
  public GUIReturn( GUIWindow pContainer )
  {
    super( pContainer );
    try
    {
      jbInit();
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Initializes the state of this instance.
   */
  private void jbInit() throws Exception
  {
    this.setLayout( layoutBase );
    this.setBackground( Colors.RTN_BACK );

    // panel: Buttons
    jPanelButtons = new GUIButtonPanel(this, theDispenser.getRecycleBin());
    
    jPanelButtons.setRequestFocusEnabled( false );
    jPanelButtons.setZeroActive( true );

    // panel: Transactions
    jPanelTransactions = new GUITransactionReturn( this );
    jPanelTransactions.setRequestFocusEnabled( false );

    // panel: BarCode
    jPanelBarCode = new GUIBarCode( this );
    jPanelBarCode.requestFocus();

    // buttons
    jButtBlank = new GUIButtonImage( Images.buttWhite, Images.buttWhite );
    jButtBlank.setDisabledIcon( Images.buttWhite );
    jButtBlank.setEnabled( false );

    this.add( jPanelButtons, new GridBagConstraints2( 0, 0, 1, 3, 5.0, 1.0,
        GridBagConstraints.WEST, GridBagConstraints.BOTH, Texts.panelMargin, 0, 0 ) );
    this.add( jButtBlank, new GridBagConstraints2( 1, 0, 1, 1, 0.1, 0.0,
        GridBagConstraints.EAST, GridBagConstraints.BOTH, Texts.buttMargin, 0, 0 ) );
    this.add( jPanelTransactions, new GridBagConstraints2( 1, 1, 1, 1, 0.1, 1.0,
        GridBagConstraints.EAST, GridBagConstraints.BOTH, Texts.transMargin, 0, 0 ) );
    this.add( jPanelBarCode, new GridBagConstraints2( 1, 2, 1, 1, 0.1, 0.0,
        GridBagConstraints.SOUTH, GridBagConstraints.BOTH, Texts.barCodeMargin, 0, 0 ) );
  }

  /**
   * Starts execution
   */
  public void start()
  {
    try
    {
      // sets status bar message
      ((GUIMenu)myContainer).setStatusMsg( Texts.RTN_STS );

      // refreshes buttons if changed
      if ( ((GUIMenu)myContainer).dbRecycleChanged )
      {
        this.jPanelButtons.refresh(theDispenser.getRecycleBin());
        this.jPanelButtons.setZeroActive( true );
        ((GUIMenu)myContainer).dbRecycleChanged = false;
      }

      // sets focus on bar code detector text field
      jPanelBarCode.setFocus();
      this.setVisible( true );
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
  }

  /**
   * Selects a product into transaction
   * @param pProd Button instance pushed
   * @return int quantity selected
   */
  public int selectProduct( Product pProd )
  {
    int li_qty = 0,
        li_alert = -1;
    String ls_quest;
    Product l_newProd;
    GUIQuantifier l_qtyDialog;
    GUIConfirmer l_sureDialog, l_extOkDialog;

    try
    {
      // stop timer, dialogs controls their own timer
      myContainer.stopTimer();
      // selects quantity calling quantifier dialog
      l_newProd = (Product) pProd.clone();
      l_qtyDialog = new GUIQuantifier( this, Texts.RTN_QTR_TIT, true );
      l_qtyDialog.setProduct( l_newProd, true );
      Config config = (Config) theSess.get(McdPrs.CONFIG);
      l_qtyDialog.start( new Long(config.getTimeout()).longValue() );
      if ( l_qtyDialog.hasExpired() ) // time expired
      {
        myContainer.timeout();
        return 0;
      }
      // li_qty keeps selected quantity
      li_qty = Integer.parseInt( l_qtyDialog.getSelection() );
      // confirms return
      if ( li_qty <= 0 )
      {
        // reset timer
        myContainer.startTimer();
        return 0;
      }
      else
      {
        // l_newProd keeps final quantity after return
        l_newProd.setQuantity( l_newProd.getQuantity() + li_qty );
        pProd.setQuantity( l_newProd.getQuantity() );

        // updates dispenser recyclebin
        theDispenser.addToRecycleBin( l_newProd );
        ((GUIMenu)myContainer).dbRecycleChanged = true;

        // registers transaction
        this.jPanelTransactions.addProduct( l_newProd, li_qty );
        this.jPanelTransactions.endTransaction( li_alert );
      }

      // reset timer
      myContainer.startTimer();
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
    // update stock
    return li_qty;
  }

  /**
   * Selects a product through external device
   * @param pCode product code selected
   * @return true if product is valid, false otherwise
   */
  public boolean externalSelection( String pCode )
  {
    try
    {
      Product l_prod = getSelectedProduct( pCode );

      if ( l_prod == null )
        return false;

      int li_qty = this.selectProduct( l_prod );
      if ( li_qty > 0 )
      {
        // updates quantity on screen
        jPanelButtons.virtualPushed( l_prod );
      }
      return true;
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
    return false;
  }

  /**
   * Returns the type of operation
   * @return type of operation
   */
  public int getType()
  {
    return McdPrs.returnId;
     
  }

  /**
   * Returns the product matching pCode
   * @param pCode product code
   * @return the Product object
   */
  public Product getSelectedProduct( String pCode )
  {
    try 
    {
      return theDispenser.getRecycledProduct( pCode );
      
    }
    catch ( Exception e )
    {
      McdError.add( e );
    }
    return null;
  }
}